package spring.superrun.record.mvc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import spring.superrun.record.mvc.dto.ManagerLoginDto;
import spring.superrun.record.mvc.dto.ManagerRegisterDto;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.service.SuperrunManagerService;
import spring.superrun.record.mvc.setup.CookieConfig;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;

@Controller
@RequestMapping("auth")
@Api("控制台登录接口")
public class AuthController {
    @Resource
    SuperrunManagerService _superrunManagerService;
    @GetMapping("login")
    @ApiOperation("登录界面")
    public String Login(){
//        model.addAttribute("");
        return "login";
    }
    @GetMapping("join")
    @ApiOperation("注册界面")
    public String Join(){return "join.html";}
    /**
     * 执行登录
     */
    @PostMapping("tologin")
    @ResponseBody
    @ApiOperation("登录接口")
    public ResponseEntity<ServiceResult> ToLogin(@RequestBody
               @Valid ManagerLoginDto dto, HttpSession session
            , HttpServletRequest request, HttpServletResponse response) {
        ServiceResult res = new ServiceResult();
        res.IsFailed("登录失败，账户或密码为空!");
        if (dto.getUserName().isEmpty()||dto.getPassword().isEmpty()){
            return new ResponseEntity<>(res,HttpStatus.BAD_REQUEST);
        }
        res = _superrunManagerService.Login(dto.getUserName().trim(),
                dto.getPassword().trim(),session, request, response);

        return new ResponseEntity<>(res
                , res.Success?HttpStatus.OK:HttpStatus.BAD_REQUEST);
    }
    /**
     * 执行注册接口
     */
    @PostMapping("tojoin")
    @ResponseBody
    @ApiOperation("注册接口")
    public ResponseEntity<ServiceResult> ToJoin(@RequestBody @Valid ManagerRegisterDto dto) {
        ServiceResult res = new ServiceResult();
        res.IsFailed("注册失败，账户或密码为空!");
        if (dto.getUserName().isEmpty()||dto.getPassword().isEmpty()
                ||dto.getEnPassword().isEmpty()){
            return new ResponseEntity<>(res,HttpStatus.BAD_REQUEST);
        }
        if (!dto.getPassword().equals(dto.getEnPassword())){
            res.IsFailed("注册失败，两次密码不一致!");
            return new ResponseEntity<>(res,HttpStatus.BAD_REQUEST);
        }
        res = _superrunManagerService.Join(dto.getUserName().trim(),
                dto.getPassword().trim());
        if (res.getCode().equals(ServiceResultCode.Repeat)){
            return new ResponseEntity<>(res,HttpStatus.PAYMENT_REQUIRED);
        }
        return new ResponseEntity<>(res
                , res.Success?HttpStatus.OK:HttpStatus.BAD_REQUEST);
    }
    /**
     * 退出登录
     */
    @RequestMapping(value = "logout")
    @ApiOperation("注销接口")
    public String Logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        // 删除session里面的用户信息
        session.removeAttribute(CookieConfig.SYSTEM_USER_SESSION);
        // 保存cookie，实现自动登录
        Cookie cookie_username = new Cookie(CookieConfig.SYSTEM_USER_SESSION, "");
        // 设置cookie的持久化时间，0
        cookie_username.setMaxAge(0);
        // 设置为当前项目下都携带这个cookie
        cookie_username.setPath(request.getContextPath());
        // 向客户端发送cookie
        response.addCookie(cookie_username);
        return Login();
    }
}
